package display;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.DefaultComboBoxModel;

import adoption.Adoption;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;

import term.Term;
import term.dept.Department;
import term.dept.Department.Code;
import term.dept.course.Course;
import term.dept.course.section.Section;

public class CourseInfo extends JFrame {

	private JPanel contentPane;
	private JComboBox depts;
	private JLabel deptName;
	private JComboBox courses;
	private Term curTerm;
	private Department curDept;
	private Course curCourse;
	private Section curSection;
	private JComboBox sections;
	private JLabel courseName;

	/**
	 * Create the frame.
	 */
	public CourseInfo(Term t) {
		curTerm = t;
		
		setTitle("Course Info");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 299, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{20, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		depts = new JComboBox();
		depts.setModel(new DefaultComboBoxModel(Code.values()));
		GridBagConstraints gbc_depts = new GridBagConstraints();
		gbc_depts.fill = GridBagConstraints.HORIZONTAL;
		gbc_depts.insets = new Insets(0, 0, 5, 5);
		gbc_depts.gridx = 0;
		gbc_depts.gridy = 0;
		contentPane.add(depts, gbc_depts);
		
		courses = new JComboBox();
		GridBagConstraints gbc_comboBox_1 = new GridBagConstraints();
		gbc_comboBox_1.gridwidth = 2;
		gbc_comboBox_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox_1.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox_1.gridx = 1;
		gbc_comboBox_1.gridy = 0;
		contentPane.add(courses, gbc_comboBox_1);
		
		sections = new JComboBox();
		GridBagConstraints gbc_courses = new GridBagConstraints();
		gbc_courses.fill = GridBagConstraints.HORIZONTAL;
		gbc_courses.insets = new Insets(0, 0, 5, 0);
		gbc_courses.gridx = 3;
		gbc_courses.gridy = 0;
		contentPane.add(sections, gbc_courses);
		
		deptName = new JLabel(((Code)depts.getSelectedItem()).getName());
		deptName.setLabelFor(depts);
		GridBagConstraints gbc_deptName = new GridBagConstraints();
		gbc_deptName.gridwidth = 2;
		gbc_deptName.fill = GridBagConstraints.HORIZONTAL;
		gbc_deptName.insets = new Insets(0, 0, 5, 5);
		gbc_deptName.gridx = 0;
		gbc_deptName.gridy = 1;
		contentPane.add(deptName, gbc_deptName);
		
		courseName = new JLabel("");
		GridBagConstraints gbc_courseName = new GridBagConstraints();
		gbc_courseName.insets = new Insets(0, 0, 0, 5);
		gbc_courseName.gridwidth = 2;
		gbc_courseName.gridx = 0;
		gbc_courseName.gridy = 2;
		contentPane.add(courseName, gbc_courseName);
		
		depts.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Code selected = (Code)depts.getSelectedItem();
				
				if (curDept == null || curDept.getCode() != selected) {
					
					deptName.setText(selected.getName());
					curDept = curTerm.getDept(selected);
					
					Map<Integer,Course> courseMap = curDept.getCourses();
					Course[] courseArray;
					if (courseMap.isEmpty()) {
						courseArray = new Course[]{null};
					}
					else {
						int i = 0;
						courseArray = new Course[courseMap.size()];
						List<Integer> crsList = new ArrayList<Integer>(courseMap.keySet());
						Collections.sort(crsList);
						for (Integer crsNum : crsList)
							courseArray[i++] = courseMap.get(crsNum);
					}
					courses.setModel(new DefaultComboBoxModel(courseArray));
					sections.setModel(new DefaultComboBoxModel());
				}
			}
			
		});
		
		courses.addActionListener(new ActionListener() {
						
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Course selected = (Course)courses.getSelectedItem();
				
				if (curCourse == null || curCourse != selected) {
					
					//set course number label
					courseName.setText(selected.getName());
					curCourse = selected;
					
					Map<String,Section> secs = curCourse.getSections();
					Section[] sectionArray;
					
					if (secs.isEmpty())
						sectionArray = new Section[]{null};
					else {
						sectionArray = new Section[secs.size()];
						List<String> secList = new ArrayList<String>(secs.keySet());
						Collections.sort(secList);
						int i = 0;
						for (String s : secList)
							sectionArray[i++] = secs.get(s);
					}
					
					sections.setModel(new DefaultComboBoxModel(sectionArray));
				}
			}
		});
		
		sections.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				Section selected = (Section)sections.getSelectedItem();
				
				if (curSection == null || curSection != selected) {
					//retrieve adoptions
					curSection = selected;
					List<Adoption> adoptions = curSection.getAdoptions();
					
					//if (adoptions.isEmpty())
						
						
				}
			}
			
		});
	}

}
